package com.pirestupppai.ui.activity;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.utils.Utils;

import butterknife.BindView;


/**
 * 修改密码
 *
 * @author: xiaoluo
 * @date: 2017-04-27 18:00
 */
public class RevisePasswordActivity extends BaseActivity {

    @BindView(R.id.old_passwd)
    EditText oldPasswdEt;
    @BindView(R.id.new_passwd)
    EditText newPasswdEt;
    @BindView(R.id.confirm_passwd)
    EditText confirmPasswdEt;
    @BindView(R.id.revise_back_btn)
    ImageView backBtn;
    @BindView(R.id.revise_save_tv)
    TextView saveTv;

    private String mOldPasswd;
    private String mNewPasswd;
    private String mConfirmPasswd;

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_revise_passwd;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initListeners() {
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        saveTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                savePasswd();
            }
        });
    }

    private void savePasswd() {
        if (validate()) {
            NetworkRequest.getInstance().revisePassword(mOldPasswd, mNewPasswd, this,
                    new TupaiSubscriber(mContext) {
                        @Override
                        protected void requestSuccess(Object o) {
                            Utils.showToast("密码修改成功");
                            finish();
                        }

                        @Override
                        protected void requestError(String error) {

                        }
                    });
        }
    }

    private boolean validate() {
        mNewPasswd = newPasswdEt.getText().toString().trim();
        mOldPasswd = oldPasswdEt.getText().toString().trim();
        mConfirmPasswd = confirmPasswdEt.getText().toString().trim();
        if (TextUtils.isEmpty(mOldPasswd)) {
            Utils.showToast("请输入原密码");
            return false;
        }

        if (TextUtils.isEmpty(mNewPasswd)) {
            Utils.showToast("请输入新密码");
            return false;
        }

        if (TextUtils.isEmpty(mConfirmPasswd)) {
            Utils.showToast("请确认密码");
            return false;
        }

        if (!mNewPasswd.equals(mConfirmPasswd)) {
            Utils.showToast("两次输入的密码不一致");
            return false;
        }

        if (mNewPasswd.length() < 6 || mNewPasswd.length() > 16) {
            Utils.showToast("密码6-16位字符");
            return false;
        }


        return true;
    }

    @Override
    protected void initLogic() {

    }

    @Override
    public void onClick(View v) {

    }
}
